package GUI.TabPanels.AdminTabs;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;

import comparators.DoubleComparator;
import comparators.IntegerComparator;

import GUI.Controller.Controller;

import main.EventHandler;

@SuppressWarnings("serial")
public class CreateRoomTypePanel extends JPanel
{	
	EventHandler handle;

	private JButton add_room_type_button;

	private JLabel name_label;
	private JTextField name_text;

	private JLabel description_label;
	private JTextField description_text;

	private JLabel beds_label;
	private JTextField beds_text;

	private JLabel cost_label;
	private JTextField cost_text;

	private JTable roomTable;

	private JLabel emptyLabel;

	private JPanel topPanel;
	private JPanel middlePanel;
	private JScrollPane scrollPane;
	
	String[] columnNames =
	{
			"ID",
			"Name",
			"Beds",
			"Description",
			"Cost"
	};

	public CreateRoomTypePanel(){
		this.setBackground(Color.PINK);
		this.setLayout(new BorderLayout());
		this.SetUpTopPanel();
		this.SetUpMiddlePanel();
		this.SetUpBottomPanel();
	}

	public void SetUpTopPanel(){
		topPanel = new JPanel();
		topPanel.setBackground(Color.PINK);
		topPanel.setLayout(new GridLayout(0, 2, -300, 7));

		name_label = new JLabel("Name:");
		name_text = new JTextField(5);
		name_text.setEditable(true);
		topPanel.add(name_label);
		topPanel.add(name_text);

		description_label = new JLabel("Description:");
		description_text = new JTextField(15);
		description_text.setEditable(true);
		topPanel.add(description_label);
		topPanel.add(description_text);

		beds_label = new JLabel("Beds:");
		beds_text = new JTextField(4);
		beds_text.setEditable(true);
		topPanel.add(beds_label);
		topPanel.add(beds_text);

		cost_label = new JLabel("Cost:");
		cost_text = new JTextField(3);
		cost_text.setEditable(true);
		topPanel.add(cost_label);
		topPanel.add(cost_text);

		add_room_type_button = new JButton("Add Room Type");
		add_room_type_button.addActionListener(new AddRoomTypeButton());

		this.add(topPanel, BorderLayout.NORTH);
	}

	public void SetUpMiddlePanel(){
		emptyLabel = new JLabel();

		middlePanel = new JPanel();
		middlePanel.setBackground(Color.PINK);
		middlePanel.add(emptyLabel);
		middlePanel.add(add_room_type_button);
		middlePanel.add(emptyLabel);

		this.add(middlePanel, BorderLayout.CENTER);
	}

	public void SetUpBottomPanel(){
		String[][] tableData = Controller.GetRoomTypesWithDetails();

		CreateTable(tableData);
	}
	
	private void CreateTable(Object[][] tableData){
		roomTable = new JTable(tableData, columnNames){
			@Override 
			public boolean isCellEditable(int row, int column) { 
				return false; 
			} 
		};
		roomTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		DefaultTableModel model = new DefaultTableModel(tableData, columnNames);

		TableRowSorter<DefaultTableModel> sorter = new TableRowSorter<DefaultTableModel>(model);

		sorter.setComparator(0, new IntegerComparator());
		sorter.setComparator(2, new IntegerComparator());
		sorter.setComparator(4, new DoubleComparator());

		roomTable.setRowSorter(sorter);
		
		roomTable.setAutoCreateRowSorter(false);
		
		roomTable.setPreferredScrollableViewportSize(new Dimension(500, 300));
		roomTable.setFillsViewportHeight(true);
		
		if(scrollPane!=null)
			remove(scrollPane);
		scrollPane = new JScrollPane(roomTable);
		this.add(scrollPane, BorderLayout.SOUTH);
	}

	class AddRoomTypeButton implements ActionListener{

		@SuppressWarnings("unused")
		@Override
		public void actionPerformed(ActionEvent arg0) {
			try
			{
				int number = Integer.parseInt(beds_text.getText());
				number = Integer.parseInt(cost_text.getText());
				if (name_text.getText().compareTo("") == 0 
						|| beds_text.getText().compareTo("") == 0 
						|| description_text.getText().compareTo("") == 0
						|| cost_text.getText().compareTo("") == 0 )
					throw new Exception();
				if (name_text.getText().length() < 3)
					throw new Exception();
				String[] args = { name_text.getText(), beds_text.getText(), description_text.getText(), cost_text.getText() };
				Controller.CreateRoomType(args);
				JOptionPane.showMessageDialog(null, "Room Type Created!");
				Update();
			}
			catch (NumberFormatException e)
			{
				JOptionPane.showMessageDialog(null, "Number of Beds and Cost must both be integers.");
			}
			catch (Exception e)
			{
				JOptionPane.showMessageDialog(null, "Cannot accept empty fields. Name must be at least 3 characters.");
			}
		}
	}
	
	private void Update()
	{
		Object[][] tableData = Controller.GetRoomTypesWithDetails();

		CreateTable(tableData);

		roomTable.updateUI();
		
		this.setVisible(false);
		this.setVisible(true);
	}
}
